package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        //得到登录用户的id,只能操作该用户自身的数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(currentId!=null,ShoppingCart::getUserId,currentId);
        if(dishId!=null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //判断购物车中是否存在该菜品
        // select * from shopping_card where user_id = ? and dish_id= ?
        ShoppingCart cartServiceOne= shoppingCartService.getOne(qw);
        if(cartServiceOne!=null){
            //number+1,执行修改操作
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //在购物车新加菜品
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    @GetMapping("/list")
    public R get(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }

    @PostMapping("/sub")
    public R remove(@RequestBody ShoppingCart shoppingCart){
        //得到登录用户的id,只能操作该用户自身的数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(currentId!=null,ShoppingCart::getUserId,currentId);
        //购物车中的是菜品
        if(dishId!=null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //购物车中的是套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //判断购物车中是否存在该菜品
        // select * from shopping_card where user_id = ? and dish_id= ?
        ShoppingCart cartServiceOne= shoppingCartService.getOne(qw);
        Integer number = cartServiceOne.getNumber();
        cartServiceOne.setNumber(number-1);
        if(number>0){
            //number-1,执行修改操作
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //删除菜品
            shoppingCartService.remove(qw);
        }
        return R.success(cartServiceOne);
    }

    @DeleteMapping("/clean")
    public R clean(){
        //delete from shoping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(qw);
        return R.success("购物车清空完成");
    }
}
